package com.diamond.web.controller.system;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.diamond.core.entity.Result;
import com.diamond.web.controller.GenericController;
import com.diamond.web.dto.AssignJsTreeDto;
import com.diamond.web.exceptions.AppException;
import com.diamond.web.model.system.Permission;
import com.diamond.web.model.system.Role;
import com.diamond.web.security.RoleSign;
import com.diamond.web.service.system.PermissionService;
import com.diamond.web.service.system.RoleService;
import com.diamond.web.utils.ApplicationInitUtils;

@RequestMapping("admin/role")
@Controller
public class RoleController extends GenericController{
	
	@Resource
	RoleService roleService;
	
	@Resource
	PermissionService permissionService;
	
	@RequestMapping("list")
	public ModelAndView toList(){
		ModelAndView mv  = getModelAndView("list");
		return mv;
	}
	
	@RequestMapping("add")
	public ModelAndView toAdd(){
		ModelAndView mv = getModelAndView("save");
		return mv;
	}
	
	@RequestMapping("edit")
	public ModelAndView toEdit(Long id){
		ModelAndView mv = getModelAndView("save");
		mv.addObject("m", roleService.selectById(id));
		return mv;
	}
	
	
	@RequestMapping("addData")
	@ResponseBody
	public Result addData(@Valid Role model,BindingResult validResult) throws AppException{
		Result result = new Result();
		if(RoleSign.SUPER_ADMIN.equals(model.getRoleSign())){
			throw new AppException(AppException.ROLE_SIGN_EXISTS);
		}
		roleService.insert(model);
		return result;
	}
	
	@RequestMapping("editData")
	@ResponseBody
	public Result editData(@Valid Role model,BindingResult validResult) throws AppException{
		if(RoleSign.SUPER_ADMIN.equals(model.getRoleSign())){
			throw new AppException(AppException.ROLE_SIGN_EXISTS);
		}
		roleService.update(model);
		return new Result();
	}
	
	@RequestMapping("delete")
	@ResponseBody 
	public Result delete(Long id){
		roleService.deleteById(id);
		return new Result();
	}
	
	@RequestMapping("listData")
	@ResponseBody
	public List<Role> list() {
		return roleService.selectList();
	}
	
	@RequestMapping("assign")
	public ModelAndView assignPermissions(Long roleId){
		ModelAndView mv = getModelAndView("assign");
		mv.addObject("roleId", roleId); 
		return mv;
	}
	
	@RequestMapping("assignData")
	@ResponseBody
	public AssignJsTreeDto  assignData(Long roleId) throws ReflectiveOperationException{
		List<Permission> hasPermission = permissionService.selectPermissionsByRoleId(roleId);
		AssignJsTreeDto dto = ApplicationInitUtils.createPermissionTree(hasPermission);
		return dto;
	}
	
	@RequestMapping("assign_form")
	@ResponseBody
	public Result assignForm(Long roleId,Long[] pids) throws ReflectiveOperationException{
		roleService.assignRolePermissions(roleId,pids);
		return new Result();
	}
}
